/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import mrp.productionPlanning.entity.BackorderEntity;
import mrp.productionPlanning.entity.DailyDetailEntity;
import mrp.productionPlanning.entity.MonthlyDetailEntity;
import mrp.productionPlanning.entity.WeeklyDetailEntity;

/**
 *
 * @author Wei Haoyang
 */
@Stateless
@LocalBean
public class ProductionPlanningSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @EJB
    private DailyDetailSessionBean ddsb;
    @EJB
    private MonthlyDetailSessionBean mdsb;
    @EJB
    private WeeklyDetailSessionBean wdsb;
    @EJB
    private BackorderSessionBean bosb;
    private List<DailyDetailEntity> days = new ArrayList<DailyDetailEntity>();

    public void scheduleMonth(MonthlyDetailEntity md) {
        days = md.getDays();
        int workdays = mdsb.countWorkDays(md);
        int demandA = md.getScheduleA() / workdays;
        int demandB = md.getScheduleB() / workdays;
        int demandC = md.getScheduleC() / workdays;
        int demandD = md.getScheduleD() / workdays;

        for (DailyDetailEntity day : days) {
            day.setScheduleA(demandA);
            day.setScheduleB(demandB);
            day.setScheduleC(demandC);
            day.setScheduleD(demandD);
            ddsb.updateDailyDetail(day);
        }
    }

    public void scheduleWeek(WeeklyDetailEntity wd) {
        days = wd.getDays();
        for (DailyDetailEntity day : days) {
            wd.setScheduleA(wd.getScheduleA() + day.getScheduleA());
            wd.setScheduleB(wd.getScheduleB() + day.getScheduleB());
            wd.setScheduleC(wd.getScheduleC() + day.getScheduleC());
            wd.setScheduleD(wd.getScheduleD() + day.getScheduleD());
            wdsb.updateWeeklyDetail(wd);
        }
    }

    public void processBackorder(BackorderEntity bo) {
        Date currentDate = Calendar.getInstance().getTime();
        List<DailyDetailEntity> period = new ArrayList<DailyDetailEntity>();
        period = ddsb.getPeriod(currentDate, bo.getDueDate());

        float timeA = bosb.quantityToTime(bo.getDemandA(), "productA");
        float timeB = bosb.quantityToTime(bo.getDemandB(), "productB");
        float timeC = bosb.quantityToTime(bo.getDemandC(), "productC");
        float timeD = bosb.quantityToTime(bo.getDemandD(), "productD");

        scheduleDemand(timeA, period, "productA");
        scheduleDemand(timeB, period, "productB");
        scheduleDemand(timeC, period, "productC");
        scheduleDemand(timeD, period, "productD");
    }

    public void scheduleDemand(float time, List<DailyDetailEntity> period, String type) {
        int amount;
        int i = 0;

        while (time > 0 || i < period.size()) {
            DailyDetailEntity day = period.get(i);
            if (time > day.getLeftCapacity()) {
                float left = day.getLeftCapacity();
                if (type.equalsIgnoreCase("productA")) {
                    amount = Integer.valueOf(Float.toString(left * 12000));
                    day.setScheduleA(day.getScheduleA() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                } else if (type.equalsIgnoreCase("productB")) {
                    amount = Integer.valueOf(Float.toString(left * 12000));
                    day.setScheduleB(day.getScheduleB() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                } else if (type.equalsIgnoreCase("productC")) {
                    amount = Integer.valueOf(Float.toString(left * 9000));
                    day.setScheduleC(day.getScheduleC() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                } else if (type.equalsIgnoreCase("productD")) {
                    amount = Integer.valueOf(Float.toString(left * 9000));
                    day.setScheduleD(day.getScheduleD() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                }
                time = time - left;
            } else if (time < day.getLeftCapacity()) {
                if (type.equalsIgnoreCase("productA")) {
                    amount = Integer.valueOf(Float.toString(time * 12000));
                    day.setScheduleA(day.getScheduleA() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                } else if (type.equalsIgnoreCase("productB")) {
                    amount = Integer.valueOf(Float.toString(time * 12000));
                    day.setScheduleB(day.getScheduleB() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                } else if (type.equalsIgnoreCase("productC")) {
                    amount = Integer.valueOf(Float.toString(time * 9000));
                    day.setScheduleC(day.getScheduleC() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                } else if (type.equalsIgnoreCase("productD")) {
                    amount = Integer.valueOf(Float.toString(time * 9000));
                    day.setScheduleD(day.getScheduleD() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                }
                time = 0;
            } else {
                if (type.equalsIgnoreCase("productA")) {
                    amount = Integer.valueOf(Float.toString(time * 12000));
                    day.setScheduleA(day.getScheduleA() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                } else if (type.equalsIgnoreCase("productB")) {
                    amount = Integer.valueOf(Float.toString(time * 12000));
                    day.setScheduleB(day.getScheduleB() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                } else if (type.equalsIgnoreCase("productC")) {
                    amount = Integer.valueOf(Float.toString(time * 9000));
                    day.setScheduleC(day.getScheduleC() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                } else if (type.equalsIgnoreCase("productD")) {
                    amount = Integer.valueOf(Float.toString(time * 9000));
                    day.setScheduleD(day.getScheduleD() + amount);
                    day.calculateCapacity();
                    day.calculateTotalTime();
                    ddsb.updateDailyDetail(day);
                }
                time = 0;
            }
            i++;
        }
        if (time > 0) {
            i = 0;
            while (time > 0 || i < period.size()) {
                DailyDetailEntity day = period.get(i);
                if (time > day.getLeftOvertime()) {
                    float left  = day.getLeftOvertime();
                    if (type.equalsIgnoreCase("productA")) {
                        amount = Integer.valueOf(Float.toString(left * 12000));
                        day.setScheduleA(day.getScheduleA() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    } else if (type.equalsIgnoreCase("productB")) {
                        amount = Integer.valueOf(Float.toString(left * 12000));
                        day.setScheduleB(day.getScheduleB() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    } else if (type.equalsIgnoreCase("productC")) {
                        amount = Integer.valueOf(Float.toString(left * 9000));
                        day.setScheduleC(day.getScheduleC() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    } else if (type.equalsIgnoreCase("productD")) {
                        amount = Integer.valueOf(Float.toString(left * 9000));
                        day.setScheduleD(day.getScheduleD() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    }
                    time = time - left;
                } else if (time < day.getLeftOvertime()) {
                    if (type.equalsIgnoreCase("productA")) {
                        amount = Integer.valueOf(Float.toString(time * 12000));
                        day.setScheduleA(day.getScheduleA() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    } else if (type.equalsIgnoreCase("productB")) {
                        amount = Integer.valueOf(Float.toString(time * 12000));
                        day.setScheduleB(day.getScheduleB() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    } else if (type.equalsIgnoreCase("productC")) {
                        amount = Integer.valueOf(Float.toString(time * 9000));
                        day.setScheduleC(day.getScheduleC() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    } else if (type.equalsIgnoreCase("productD")) {
                        amount = Integer.valueOf(Float.toString(time * 9000));
                        day.setScheduleD(day.getScheduleD() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    }
                    time = 0;
                } else {
                    if (type.equalsIgnoreCase("productA")) {
                        amount = Integer.valueOf(Float.toString(time * 12000));
                        day.setScheduleA(day.getScheduleA() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    } else if (type.equalsIgnoreCase("productB")) {
                        amount = Integer.valueOf(Float.toString(time * 12000));
                        day.setScheduleB(day.getScheduleB() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    } else if (type.equalsIgnoreCase("productC")) {
                        amount = Integer.valueOf(Float.toString(time * 9000));
                        day.setScheduleC(day.getScheduleC() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    } else if (type.equalsIgnoreCase("productD")) {
                        amount = Integer.valueOf(Float.toString(time * 9000));
                        day.setScheduleD(day.getScheduleD() + amount);
                        day.calculateCapacity();
                        day.calculateTotalTime();
                        ddsb.updateDailyDetail(day);
                    }
                    time = 0;
                }
                i++;
            }
        }

        return;
    }
}
